//
//  Kylin_TopicCell.m
//  03-BuDeJie
//
//  Created by Kylin on 16/3/1.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_TopicCell.h"
#import "Kylin_TopicItem.h"
#import <UIImageView+WebCache.h>
#import "Kylin_TopicAudioView.h"
#import "Kylin_TopicPictureView.h"
#import "Kylin_TopicVideoView.h"

@interface Kylin_TopicCell ()
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *passTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_Label;
@property (weak, nonatomic) IBOutlet UIButton *moreButton;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;

/** 最热评论整体*/
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
/** 最热评论内容*/
@property (weak, nonatomic) IBOutlet UILabel *topCmtLabel;

// 中间控件
/**图片控件*/
@property(nonatomic,weak)Kylin_TopicPictureView *pictureView;
/**声音控件*/
@property(nonatomic,weak)Kylin_TopicAudioView *audioView;
/**视频控件*/
@property(nonatomic,weak)Kylin_TopicVideoView *videoView;
@end
@implementation Kylin_TopicCell

#pragma mark- 赖加载中间控件
- (Kylin_TopicPictureView *)pictureView{
    if (!_pictureView) {
        Kylin_TopicPictureView *pictureView = [Kylin_TopicPictureView viewForXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (Kylin_TopicAudioView *)audioView{
    if (!_audioView) {
        Kylin_TopicAudioView *audioView = [Kylin_TopicAudioView viewForXib];
        [self.contentView addSubview:audioView];
        _audioView = audioView;
    }
    return _audioView;
}

- (Kylin_TopicVideoView *)videoView{
    if (!_videoView) {
        Kylin_TopicVideoView *videoView = [Kylin_TopicVideoView viewForXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}
// 从XIB中加载时设置背景图片
- (void)awakeFromNib{
    self.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

#pragma mark- set方法设置显示数据
- (void)setTopicItem:(Kylin_TopicItem *)topicItem{
    
    _topicItem = topicItem;
    
//    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topicItem.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    [self.profileImageView setIconWithURL:topicItem.profile_image];
    self.nameLabel.text = topicItem.name;
    self.passTimeLabel.text = topicItem.passtime;
    self.text_Label.text = topicItem.text;
    
    
    // 设置底部按钮的文字
    [self setupButton:self.dingButton number:topicItem.ding placeholder:@"顶"];
    [self setupButton:self.caiButton number:topicItem.cai placeholder:@"踩"];
    [self setupButton:self.shareButton number:topicItem.repost placeholder:@"分享"];
    [self setupButton:self.commentButton number:topicItem.comment placeholder:@"评论"];
    
    if (topicItem.type == Kylin_TopicTypePicture) {
        self.audioView.hidden = YES;
        self.videoView.hidden = YES;
        self.pictureView.hidden = NO;
        self.pictureView.topicItem = topicItem;
    }else if(topicItem.type == Kylin_TopicTypeAudio) {
        self.audioView.hidden = NO;
        self.videoView.hidden = YES;
        self.pictureView.hidden = YES;
        self.audioView.topicItem = topicItem;
    }else if(topicItem.type == Kylin_TopicTypeVideo) {
        self.audioView.hidden = YES;
        self.videoView.hidden = NO;
        self.pictureView.hidden = YES;
        self.videoView.topicItem = topicItem;
    }else if (topicItem.type == Kylin_TopicTypeWrod){
        self.audioView.hidden = YES;
        self.videoView.hidden = YES;
        self.pictureView.hidden = YES;
    }
    
    
    // 有最热评论最热评论
    if (topicItem.top_cmt.count) {
        
        self.topCmtView.hidden = NO;
        NSDictionary *dict = topicItem.top_cmt.firstObject;
        NSString *content = dict[@"content"];
        
        if (content.length == 0) {
            content = @"[语音信息]";
        }
        
        NSString *userName = dict[@"user"][@"username"];
       self.topCmtLabel.text = [NSString stringWithFormat:@"%@ : %@",userName,content];
    }else{
        self.topCmtView.hidden = YES;
    }
}

/** 设置子控件的frame */
- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (self.topicItem.type == Kylin_TopicTypePicture) {
        self.pictureView.frame = self.topicItem.middleFrame;
    }else if (self.topicItem.type == Kylin_TopicTypeAudio){
        self.audioView.frame = self.topicItem.middleFrame;
    }else if (self.topicItem.type == Kylin_TopicTypeVideo){
        self.videoView.frame = self.topicItem.middleFrame;
    }
}

- (void)setupButton:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder{
    
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万",number / 10000.0] forState:UIControlStateNormal];
    }else if (number > 0){
        [button setTitle:[NSString stringWithFormat:@"%zd",number ] forState:UIControlStateNormal];
    }else{
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

// 重写cell  frame set方法
- (void)setFrame:(CGRect)frame{
    frame.size.height -= Kylin_CommonMargin;
    [super setFrame:frame];
}
@end
